package Aleehot100;

/**
 * ClassName: c07
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/21 20:23
 * @Version 1.0
 * @Since 1.0
 */
public class c07 {
    public static void main(String[] args) {
        int[] arr = {4,2,0,3,2,5};
        System.out.println(trap(arr));
    }


    /**
     * 接雨水问题
     * @param height
     * @return
     */
    public static int trap(int[] height) {
        int l = 0, r = height.length - 1;
        int lmax = height[0], rmax = height[r], res = 0;
        while(l < r) {
            if(lmax > rmax) {
                res += Math.max(0, rmax - height[-- r]);
                rmax = Math.max(rmax, height[r]);
            } else {
                res += Math.max(0, lmax - height[++ l]);
                lmax = Math.max(lmax, height[l]);
            }
        }
        return res;
    }

}
